# configs/config.py
from typing import Dict, Any
import numpy as np
import airsim
from code_test.map_date_get import get_world_size

class Config:
    """管理AirSim强化学习环境的配置参数"""

    def __init__(self):

        self.x_boundary_limit, self.y_boundary_limit, self.z_boundary_limit = get_world_size()

        self.config = {
            "environment": {
                "vehicle_names": ["Car1", "Car2", "Car3"],
                "ip": "127.0.0.1", # 127.0.0.1   175.2.157.47
                "port": 41451,
                "render_mode": "human",
                "max_episode_steps": 600,
                "max_fps": 20,
                "x_boundary_limit": self.x_boundary_limit,
                "y_boundary_limit": self.y_boundary_limit,
                "z_boundary_limit": self.z_boundary_limit,
                "arrived_threshold": 2, # 目标点范围
                "collision_severity_threshold": 0.02, # 车辆碰撞阈值
                "roll_threshold": np.pi / 2, # 翻滚重置阈值
                "pitch_threshold": np.pi / 2,  # 俯仰角度重置阈值
            },
            "observation": {
                "include_position": True,
                "include_velocity": True,
                "include_orientation": True,
                "include_radar": True,
                "include_lidar": False,
                "radar_features": 10,
                "normalize": True
            },
            "action": {
                "continuous": True,
                "throttle_range": [0.0, 1.0],  # 加速度 连续控制 前进后退
                "steering_range": [-0.7, 0.7], # 转向
                "brake_range": [0.0, 1.0] # 非线性刹车
            },
            "rewards": {
                "formation_type": "triangle",
                "formation_interval": 8.0,  # 间隔
                "formation_side_length": 4.0,  # 边长
                "formation_radius":4.0, #
                "formation_max_distance": 10,
                "safe_distance": 2.0,
                "formation_weight": 6.0,
                "collision_penalty": -50,
                "collision_threshold": 0.01,
            },
            "termination": {
                "collision_terminates": True,
                "out_of_bounds_terminates": True,
                "boundary": 200.0,
                "max_steps_terminates": True
            },
            "goal": {
                "enabled": True,
                "position": [100, 0, -0.61],
                # "position": None # None则随机生成 目标点
            },
            "airsim_gym3": {
                "num_agents": 3, # 智能体个数 和settings.json一致
                "img_width": 800, # 和settings.json可以不一致，输入网阔使用
                "img_height": 600, # 和settings.json可以不一致，输入网阔使用
                "lidar_points": 10000, # 和settings.json可以不一致，输入网阔使用
                "use_brake": False,  # 是否启用刹车，
                "reward_params": {
                    "distance_weight": 0.5,
                    "collision_penalty": -10.0,
                    "step_reward": -0.1,
                    "max_speed_reward": 0.2,
                    "speed_threshold": 5.0
                }

            },
            "Vision_process": {
                "depth_scale": 1000.0,
                "max_valid_depth": 100.0,
                "num_distance_sensors": 16,# 距离传感器数量 和settings.json一致
                "DistanceSensor_max": 200 # 距离传感器探测距离 和settings.json一致
                # "lidar_num_sectors": 16 # 雷达扇区分类

            }
        }

    def get(self, key: str, default: Any = None) -> Any:
        """获取配置值"""
        keys = key.split('.')
        value = self.config
        for k in keys:
            if k in value:
                value = value[k]
            else:
                return default
        return value

    def set(self, key: str, value: Any) -> None:
        """设置配置值"""
        keys = key.split('.')
        config = self.config
        for k in keys[:-1]:
            if k not in config:
                config[k] = {}
            config = config[k]
        config[keys[-1]] = value

    def to_dict(self) -> Dict[str, Any]:
        """将配置转换为字典"""
        return self.config